const db = wx.cloud.database() //在客户端获取数据库的引用
const app = getApp()    //获取小程序实例
Page({
  data: {
    userInfo: {}, //用户信息
    tag: false, //false 未登录;true 已登录
  },
  onLoad() {
    //获取用户信息
    var userInfo = app.globalData.userInfo;
    if (userInfo == null) {
      this.setData({
        tag: false
      })
      //给app.js定义一个函数getInfo
      app.getInfo = (res => {
        this.setData({
          tag:true,
          userInfo:res
        })
        
      })
    } else {
      this.setData({
        tag: true,
        userInfo
      })
    }
  },

  //点击调用云函数
  myClick() {
    wx.cloud.callFunction({
      name: "login",
      data: {
        username: "张三"
      },
      success: res => {
        var openid = res.result.openid; //获取用户openid
        wx.setStorageSync("openid", openid)
      },
      fail: err => {
        console.log(err)
      }
    })
  },
  //批量删除
  handleDel() {
    wx.cloud.callFunction({
      name: "batchDel",
      data: {
        openid: wx.getStorageSync('openid')
      },
      success: res => {
        console.log(res)
      },
      fail: err => {
        console.log(err)
      }

    })
  },
  //获取用户公开信息
  myInfo(e) {
    var userInfo = e.detail.userInfo;
    if (userInfo != undefined) {
      this.setData({
        tag: true,
        userInfo
      })
    }

  }
})